// pages/release_requ/release_requ.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    maxWord: 200,
    requ_name:'',
    requ_age:'',
    requ_sex:'',
    requ_school:'',
    requ_grade:'',
    requ_identity:'',
    requ_address:'',
    requ_guardian_name:'',
    requ_guardian_age:'',
    requ_guardian_relation:'',
    requ_income:'',
    requ_income_source:'',
    requ_user_tele:'',
    requ_text:'',
    requ_file:'',
    requ_study:'',
    requ_ethnic:'',
    sugg_filePath: [],
    sugg_chart_id:'',
    requ_file:''

  },
  limitWord:function(e){
    var value = e.detail.value;
    //解析字符串长度转换成整数。
    var wordLength = parseInt(value.length); 
    if (this.data.maxWord < wordLength) {
      return ;
    }
    this.setData({
      currentWord: wordLength 
    });
  },
  
  handleChooseImg() {
    var that = this;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      sizeType: ['original', 'compressed'],
      camera: 'back',
      success(res) {
        var tempFiles = res.tempFiles;
        for (var i = 0; i < tempFiles.length; i++) {
          that.setData({
            imgs: [...that.data.imgs, tempFiles[i].tempFilePath],
            sugg_filePath: [...that.data.sugg_filePath, tempFiles[i].tempFilePath]
          });
        }
      }
    });
  },
  checkPhoneNum(phoneNumber) {
    // 正则表达式校验手机号格式
    if (!/^1[3456789]\d{9}$/.test(phoneNumber)) {
      // 手机号格式不正确，提示用户
      return false;
    }
    return true;
  },
  deleteImg: function (e) {
    wx.showModal({
      title: "提示",
      content: "是否删除",
      success: function (res) {
        if (res.confirm) {
          this.data.imgs.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgs: this.data.imgs
          });
        }
      }
    });
  },
  findEmptyKeys(jsonObj) {
    return Object.keys(jsonObj).filter(key => !jsonObj[key] || jsonObj[key] === '');
  },

  formsubmit: function (e) {
    var that = this;
    const formData = e.detail.value;
    console.log(formData)
    const judge = this.findEmptyKeys(formData)
    if(judge.includes( "requ_name","requ_age","requ_sex","requ_school","requ_grade","requ_identity", "requ_address","requ_guardian_name", "requ_guardian_age", "requ_guardian_relation","requ_income","requ_income_source", "requ_user_tele",  "requ_text", "requ_file","requ_ethnic","sugg_filePath",)){
      wx.showModal({
        title: '提示',
        content: '请完成所有必填项！',
        showCancel: false,
      });
    }else{
      if(!this.checkPhoneNum(formData.requ_user_tele)){
        wx.showModal({
          title: '提示',
          content: '手机号不正确，请检查！',
          showCancel: false,
        });
      }else{
        this.setData({
          requ_name:formData.requ_name,
          requ_age:formData.requ_age,
          requ_sex:formData.requ_sex,
          requ_school:formData.requ_school,
          requ_grade:formData.requ_grade,
          requ_identity:formData.requ_identity,
          requ_address:formData.requ_address,
          requ_guardian_name:formData.requ_guardian_name,
          requ_guardian_age:formData.requ_guardian_age,
          requ_guardian_relation:formData.requ_guardian_relation,
          requ_income:formData.requ_income,
          requ_income_source:formData.requ_income_source,
          requ_user_tele:formData.requ_user_tele,
          requ_text:formData.requ_text,
          requ_file:this.data.filePath,
          requ_ethnic:formData.requ_ethnic,
          requ_study:formData.requ_study
        });
        
          console.log({
            // 'sugg_media': this.data.sugg_media,
            'requ_name':this.data.requ_name,
            'requ_age':this.data.requ_age,
            'requ_sex':this.data.requ_sex,
            'requ_school':this.data.requ_school,
            'requ_grade':this.data.requ_grade,
            'requ_identity':this.data.requ_identity,
            'requ_address':this.data.requ_address,
            'requ_guardian_name':this.data.requ_guardian_name,
            'requ_guardian_age':this.data.requ_guardian_age,
            'requ_guardian_relation':this.data.requ_guardian_relation,
            'requ_income':this.data.requ_income,
            'requ_income_source':this.data.requ_income_source,
            'requ_user_tele':this.data.requ_user_tele,
            'requ_text':this.data.requ_text,
            'requ_file':this.data.requ_file,
            'requ_ethnic':this.data.requ_ethnic,
            'requ_study':this.data.requ_study
          })

          
          wx.request({
            url: 'http://116.205.102.248:8080/user/UserRequirementCreate/',
            // filePath: this.data.sugg_filePath,
            // name: "sugg_media",
            method: 'POST',
            header: {
              'Authorization': wx.getStorageSync('token'),
              "content-type" :'application/x-www-form-urlencoded'
            },
            data: {
                // 'sugg_media': this.data.sugg_media,
                'requ_name':this.data.requ_name,
                'requ_age':this.data.requ_age,
                'requ_sex':this.data.requ_sex,
                'requ_school':this.data.requ_school,
                'requ_grade':this.data.requ_grade,
                'requ_identity':this.data.requ_identity,
                'requ_address':this.data.requ_address,
                'requ_guardian_name':this.data.requ_guardian_name,
                'requ_guardian_age':this.data.requ_guardian_age,
                'requ_guardian_relation':this.data.requ_guardian_relation,
                'requ_income':this.data.requ_income,
                'requ_income_source':this.data.requ_income_source,
                'requ_user_tele':this.data.requ_user_tele,
                'requ_text':this.data.requ_text,
                'requ_file':this.data.requ_file,
                'requ_ethnic':this.data.requ_ethnic,
                'requ_study':this.data.requ_study

              },
              success: function (res) {
                console.log("建议", res)
                if (res.statusCode == 200) {
                  
                  // for(let i = 0; i < that.data.imgs.length; i++){
                  //   console.log("上传图片：",i)
                  //   wx.uploadFile({
                  //                 url: 'http://10.21.235.116:8000/user/UserSuggestionPicture/' + that.data.sugg_chart_id + '/',
                  //                 filePath: that.data.sugg_filePath[i],
                  //                 name: "sugg_media",
                  //                 header: {
                  //                   'Authorization': wx.getStorageSync('token'),
                  //                   "content-type": "multipart/form-data"
                  //                 },
                  //                 formData: {
                  //                   'sugg_media':that.data.sugg_filePath[i],
                      
                  //                 },
                  //                 success: function (res) {
                  //                   console.log("建议图片", res)
                  //                   if (res.statusCode == 200) {
                  //                     wx.showModal({
                  //                       title: '提示',
                  //                       content: '提交成功，请耐心等待回复。',
                  //                       showCancel: false,
                  //                       success: function () {
                  //                         wx.switchTab({
                  //                           url: '../index/index',
                  //                         });
                  //                       },
                  //                     });
                  //                   }
                  //                 },
                  //                 fail: function (err) {
                  //                   console.log(err)
                  //                     wx.showModal({
                  //                       title: '提示',
                  //                       content: '提交失败，请重试！',
                  //                       showCancel: false,
                  //                     });
                  //                 }
                  //               });
                  // }
                  wx.showModal({
                    title: '提示',
                    content: '提交成功，请耐心等待回复。',
                    showCancel: false,
                    success: function () {
                      wx.switchTab({
                        url: '../index/index',
                      });
                    },
                  });
                }
              },
              fail: function (err) {
                console.log(err)
                wx.showModal({
                  title: '提示',
                  content: '提交失败，请重试！',
                  showCancel: false,
                });
              }
          })
          console.log(this.data.imgs)
          
          
        
 
      }
    }
  },

})